package com.swsm.utils;

import java.net.URL;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class EhCache {
	
	private final static String RESOURCE_PATH = "/ehcache.xml";
	private final CacheManager cacheManager;
	private Cache cache;
	
	private EhCache() {
		URL url = getClass().getResource(RESOURCE_PATH);
		cacheManager = CacheManager.create(url);
		cache = cacheManager.getCache("studentPermissionCache");
	}
	
	public static EhCache getInstance() {
		return CacheHandler.instance;
	}
	
	private static class CacheHandler {
		private static final EhCache instance = new EhCache();
	}
	
	public void put(Object key, Object value) {
		Element element = new Element(key, value);
		cache.put(element);
	}
	
	public Object get(Object key) {
		Element element = cache.get(key);
		return element == null ? null : element.getObjectValue();
	}
	
	public void remove(Object key)  {
		cache.remove(key);
	}

}
